package com.trafficsys.modules.licenseplaterecognition.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.trafficsys.modules.entity.Ticket;
import com.trafficsys.modules.licenseplaterecognition.vo.TicketVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface TicketDao extends BaseMapper<Ticket> {
    @Select("SELECT t.id,t.car_no,p.name police_name,r.rule_name,r.money,r.cut_score,t.create_time FROM ticket t " +
            "JOIN police p " +
            "ON t.police_id=p.card_id " +
            "JOIN rule r " +
            "ON t.rule_id = r.id")
    IPage<TicketVo> getAllTickets(IPage<TicketVo> page);

    @Select("SELECT t.id,t.car_no,p.name police_name,r.rule_name,r.money,r.cut_score,t.create_time FROM ticket t " +
            "JOIN police p " +
            "ON t.police_id=p.card_id " +
            "JOIN rule r " +
            "ON t.rule_id = r.id " +
            "WHERE p.card_id=#{card_id}")
    IPage<TicketVo> getSelfTickets(IPage<TicketVo> page, @Param("card_id") String card_id);

    @Select("SELECT d.phone FROM driver d " +
            "JOIN travel_card tc " +
            "ON  d.card_id = tc.card_id " +
            "WHERE tc.car_no =#{carNo}")
    String getPhoneByCarNo(String carNo);

    @Select("select * from ticket where id = #{id}")
    Ticket getTicketById(@Param("id")String id);

    @Update("update ticket set car_no = #{carNo}, police_id = #{policeId},rule_id = #{ruleId}, create_time = #{createTime}, " +
            "update_time = #{updateTime},state = #{state} where id = #{id}")
    void updateTicketById(Ticket ticket);
}
